package com.alstudio.core.app;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.utils.android.AndroidUtils;
import com.alstudio.utils.android.net.download.ALHttpDownloadListener;
import com.alstudio.utils.android.net.download.ALHttpDownloadManager;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask;
import com.alstudio.utils.android.net.download.ALHttpDownloadTask.ALHttpDownloadErrorCode;
import com.alstudio.utils.android.notification.ALNotification;
import com.alstudio.utils.android.notification.ALNotification.ALNotificationType;
import com.alstudio.utils.log.ALLog;
import com.loovee.imaohu.R;

public class ALUpdateApkManager {

	private static Context mContext;
	private final static int UPDATE_APK_NOTICE_ID = 2000;
	
	private static boolean isDownloading = false;

	public static void startDownloadApk(final Context context,
			final String url, final String path, final String name) {
		isDownloading = true;
		mContext = context;
		ALHttpDownloadManager.startDownload(context, url, path, name,
				mApkDownloadListener);
	}
	
	/**
	 * @return the isDownloading
	 */
	public static boolean isDownloading() {
		return isDownloading;
	}

	/**
	 * 用于接收下载APK更新的事件通知
	 */
	private static ALHttpDownloadListener mApkDownloadListener = new ALHttpDownloadListener() {

		private ALNotification n = null;
		private int progressed = 0;

		@Override
		public void onUpdateProcess(final ALHttpDownloadTask info) {
			// TODO Auto-generated method stub
			new Handler().post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (progressed != info.getDownloadPercent()) {
						progressed = info.getDownloadPercent();
						updateProgressNotification(mContext, n,
								info.getDownloadPercent(), info.getFilePath(),
								info.getFileName());
					}
				}
			});

		}

		@Override
		public void onDownloadFinish(ALHttpDownloadTask info) {
			// TODO Auto-generated method stub
			isDownloading = false;

			ALLocalEnv.getLocalEnv().getNotificationCenterManager()
					.cancelNotification(n);
			updateProgressNotification(mContext, n, 100, info.getFilePath(),
					info.getFileName());
		}

		@Override
		public void onPreDownload(final String name) {
			// TODO Auto-generated method stub
			ALLog.d("准备开始下载");
			n = showNotification(mContext, name);
		}

		@Override
		public void onDownloadFail(ALHttpDownloadErrorCode code,
				final String name, ALHttpDownloadTask info) {
			// TODO Auto-generated method stub
			ALLog.d("下载失败:" + code);
			isDownloading = false;

			showDownloadFailNotification(mContext, n, code, name);
		}
	};

	private static ALNotification showNotification(Context context,
			final String name) {
		{
			//不要声音
			ALNotification builder = new ALNotification(context,
					ALNotificationType.AL_NOTIFICATION_TYPE_PROGRESS,
					String.format(
							mContext.getString(R.string.TxtPrepareUpdateApk),
							name),true);
			if (!TextUtils.isEmpty(name)) {
				builder.setContentTitle(String.format(
						mContext.getString(R.string.TxtPrepareUpdateApk), name));
			} else {
				builder.setContentTitle("正在获取文件名");
			}
			builder.setContentText(mContext
					.getString(R.string.TxtDownloadingApkProgress) + " 0%");

			builder.setSmallIcon(R.drawable.ic_launcher);
			builder.setId(UPDATE_APK_NOTICE_ID);

			builder.setContentIntent(ALNotification.backToApp(context));
			builder.setProgress(100, 0, false);

			ALLocalEnv.getLocalEnv().getNotificationCenterManager()
					.showNotification(builder);

			return builder;
		}
	}

	private static void updateProgressNotification(Context context,
			ALNotification notification, int progress, String path, String name) {

		notification.setProgress(100, progress, false);
		notification.setContentTitle(String.format(
				mContext.getString(R.string.TxtDownloadingApk), name));

		notification.setContentText(mContext
				.getString(R.string.TxtDownloadingApkProgress)
				+ " "
				+ progress
				+ "%");
		notification.setSound(null);

		if (progress == 100) {
			notification.setContentTitle(String.format(
					mContext.getString(R.string.TxtDownloadApkFinish), name));

			notification.setTicker(String.format(
					mContext.getString(R.string.TxtDownloadApkFinish), name));


			AndroidUtils.installApk(context, path);
		}

		ALLocalEnv.getLocalEnv().getNotificationCenterManager()
				.showNotification(notification);
	}

	private static void showDownloadFailNotification(Context context,
			ALNotification notification, ALHttpDownloadErrorCode code,
			String name) {

		ALLocalEnv.getLocalEnv().getNotificationCenterManager()
				.cancelNotification(notification);
		notification.setType(ALNotificationType.AL_NOTIFICATION_TYPE_NORMAL);
		notification.setTicker(String.format(
				mContext.getString(R.string.TxtDownloadApkFail), name));
		notification.setContentTitle(mContext
				.getString(R.string.TxtDownloadApkFail));

		switch (code) {
		case AL_HTTP_DOWNLOAD_TYPE_ERROR_EXTENAL_STORAGE_OUT_OF_RANGE:
			notification.setContentTitle(String.format(mContext
					.getString(R.string.TxtDownloadApkFailForExternalMemory),
					name));
			break;
		default:
			notification.setContentTitle(String.format(
					mContext.getString(R.string.TxtDownloadApkFailForNetWork),
					name));
			break;
		}

		ALLocalEnv.getLocalEnv().getNotificationCenterManager()
				.showNotification(notification);
	}
}
